//
//  ViewController.m
//  NSOperation
//
//  Created by fangyuan ding on 2020/10/26.
//  Copyright © 2020 fangyuan ding. All rights reserved.
//

#import "ViewController.h"
#import "WHOperation.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self testNSInvocationOperation];
//    [self testNSBlockOperation];
//    [self testNSBlockOperationExecution];
//    [self testWHOperation];
//    [self testOperationQueue];
//    [self testAddOperationWithBlock];
//    [self testMaxConcurrentOperationCount];
    [self testAddDependency];
}
- (void)testNSInvocationOperation{
    // 创建NSInvocationOperation
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]
    initWithTarget:self selector:@selector(invocationOperation) object:nil];
    // 开始执行操作
    [invocationOperation start];
}
//程序在主线程执行，没有开启新线程
- (void)invocationOperation {
NSLog(@"NSInvocationOperation包含的任务，没有加入队列========%@", [NSThread currentThread]);
}
- (void)testNSBlockOperation{
    // 把任务放到block中
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"NSBlockOperation包含的任务，没有加入队列========%@", [NSThread currentThread]);
    }];
    [blockOperation start];
}
- (void)testNSBlockOperationExecution{
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"NSBlockOperation运用addExecutionBlock主任务========%@", [NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
    NSLog(@"NSBlockOperation运用addExecutionBlock方法添加任务1========%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
    NSLog(@"NSBlockOperation运用addExecutionBlock方法添加任务2========%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
    NSLog(@"NSBlockOperation运用addExecutionBlock方法添加任务3========%@",[NSThread currentThread]);
    }];
    [blockOperation start];
}
- (void)testWHOperation{
    WHOperation *operation = [[WHOperation alloc] init];
//    [operation start];
}
- (void)testOperationQueue{
    // 创建队列，默认并发
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // 创建操作，NSInvocationOperation
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]
    initWithTarget:self selector:@selector(invocationOperationAddOperation) object:nil];
    // 创建操作，NSBlockOperation
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"addOperation把任务添加到队列======%@", [NSThread currentThread]);
    }
    }];
    [queue addOperation:invocationOperation];
    [queue addOperation:blockOperation];
}
- (void)invocationOperationAddOperation{
    NSLog(@"invocationOperation===aaddOperation把任务添加到队列====%@", [NSThread currentThread]);
}
- (void)testAddOperationWithBlock{
    // 创建队列，默认并发
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // 添加操作到队列
    [queue addOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"addOperationWithBlock把任务添加到队列======%@", [NSThread currentThread]);
    }
    }];
}
- (void)testMaxConcurrentOperationCount{
    // 创建队列，默认并发
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // 最大并发数为1，串行
//    queue.maxConcurrentOperationCount = 1;
    // 最大并发数为2，并发
     queue.maxConcurrentOperationCount = 2;
    // 添加操作到队列
    [queue addOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"addOperationWithBlock把任务添加到队列1======%@", [NSThread currentThread]);
    }
    }];
    // 添加操作到队列
    [queue addOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"addOperationWithBlock把任务添加到队列2======%@", [NSThread currentThread]);
    }
    }];
    // 添加操作到队列
    [queue addOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"addOperationWithBlock把任务添加到队列3======%@", [NSThread currentThread]);
    }
    }];
}
- (void)testAddDependency{
    // 并发队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // 操作1
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
    for (int i = 0; i < 3; i++) {
    NSLog(@"operation1======%@", [NSThread currentThread]);
    }
    }];
    // 操作2
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"****operation2依赖于operation1，只有当operation1执行完毕，operation2才会执行****");
    for (int i = 0; i < 3; i++) {
    NSLog(@"operation2======%@", [NSThread currentThread]);
    }
    }];
    // 使操作2依赖于操作1
    [operation2 addDependency:operation1];
    // 把操作加入队列
    [queue addOperation:operation1];
    [queue addOperation:operation2];
}
@end
